home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / MACNACE / MACNACE source / shell.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-25  |  3.0 KB  |  180 lines  |  [TEXT/CWIE]

  1. #include <iostream.h>
  2. #include <string.h>
  3. #include <SIOUX.h>
  4.  
  5. #include "shell.h"
  6.  
  7. bool    quitting = false;
  8. bool    nocc = true;
  9. string    curd;
  10.  
  11. int main () {
  12.     init ();
  13.  
  14.     cout << "Welcome to M.A.C.N.A.C.E." << endl;
  15.  
  16.     while (!quitting) {
  17.         if (login ())
  18.             shell ();
  19.     };
  20.     
  21.     cout << "session ended." << endl;
  22. };
  23.  
  24. bool login () {
  25.     string    id, passwd;
  26.     
  27.     cout << "login: ";
  28.     cin >> id;
  29.     cout << "password: ";
  30.     cin >> passwd;
  31.     
  32.     if (id == "root" && passwd == "2getinit") {
  33.         cout << "Yow!" << endl;
  34.         return true;
  35.     }
  36.     else {
  37.         cout << "login failed." << endl << endl;
  38.         return false;
  39.     };
  40. };
  41.  
  42. void shell () {
  43.     char    cmds [1000];
  44.     string    cmdline;
  45.     long    nextpos;
  46.     
  47.     cmds [0] = '\0';
  48.     cout << "# ";
  49.     cout.flush;
  50.     cin.getline (cmds, 1000);
  51.     cin.getline (cmds, 1000);
  52.     cmdline = cmds;
  53.     
  54.     while (!quitting) {
  55.         if (cmdline == "exit") {
  56.             quitting = true;
  57.             continue;
  58.         };
  59.             
  60.         string    cmd = parse (cmdline, nextpos);
  61.         cmdline = cmdline.substr (nextpos);
  62.         
  63.         if (cmd == "ps")
  64.             ps (cmdline);
  65.         else if (cmd == "ls")
  66.             ls (cmdline);
  67.         else if (cmd == "kill")
  68.             kill (cmdline);
  69.         else if (cmd == "cat")
  70.             cat (cmdline);
  71.         else if (cmd == "cc")
  72.             cc (cmdline);
  73.         else if (cmd == "cd")
  74.             cd (cmdline);
  75.         else if (cmd == "pwd")
  76.             cout << curd << endl;
  77.         else if (cmd == "a.out" && curd == "/src" && !nocc)
  78.             cout << "hello, world." << endl;
  79.         else
  80.             bad (cmd);
  81.         
  82.         cmdline = "";
  83.         cout << "# ";
  84.         cout.flush;
  85.         cin.getline (cmds, 1000);
  86.         cmdline = cmds;
  87.         
  88.     };
  89. };
  90.  
  91. string parse (string line, long& rp) {
  92.     string    cmd;
  93.     
  94.     long    p;
  95.     p = line.find (" ");
  96.     if (p == string::npos) {
  97.         rp = line.length ();
  98.         return line;
  99.     };
  100.     rp = p + 1;
  101.     cmd = line.substr (0, p);
  102.     return cmd;
  103. };
  104.  
  105. void ps (string) {
  106.     string    *pname;
  107.     FindRunningTargetApp ();
  108.     cout << "PID  UID   NAME" << endl;
  109.     for (long i = 0; i < proc; i++) {
  110.         pname = new string (strs [i]);
  111.         cout << setw (3) << i << " root   " << *pname << endl;
  112.         delete pname;
  113.     };
  114. };
  115.  
  116. void ls (string dir) {
  117.     string    wd;
  118.     
  119.     if (dir == "")
  120.          wd = curd;
  121.      else
  122.          wd = dir;
  123.          
  124.      if (wd == "/")
  125.          cout << "bin/   etc/   src/   usr/   " << endl;
  126.      else if (wd == "/src" && nocc)
  127.          cout << "hello.c" << endl;
  128.      else if (wd == "/src" && !nocc)
  129.          cout << "a.out*  hello.c" << endl;
  130. };
  131.  
  132. void kill (string) {
  133.  
  134. };
  135.  
  136. void cat (string file) {
  137.     if (file == "hello.c" && curd == "/src") {
  138.         cout << "#include <stdio.h>" << endl;
  139.         cout << endl;
  140.         cout << "int main () {" << endl;
  141.         cout << "    printf (" << '"' << "hello, world." << '\\' << "n" << '"' << ");" << endl;
  142.         cout << "};" << endl;
  143.     };
  144. };
  145.  
  146. void cc (string) {
  147.     nocc = false;
  148. };
  149.  
  150. void cd (string dir) {
  151.     if (dir == "") {
  152.         curd = "/";
  153.         return;
  154.     };
  155.     
  156.     long    p;
  157.     p = dir.find ("/");
  158.     if (p == 0)
  159.         curd = dir;
  160.     else
  161.         curd += dir;
  162. };
  163.  
  164. void bad (string cmd) {
  165.     if (cmd != "")
  166.     cout << cmd << " :comand not recognised." << endl;
  167. };
  168.  
  169. void init () {
  170.     curd = "/";
  171.     SIOUXSettings.asktosaveonclose = false;
  172.     SIOUXSettings.fontid = kFontIDCourier;
  173.     SIOUXSettings.fontsize = 14;
  174.     SIOUXSettings.toppixel = 20;
  175.     SIOUXSettings.leftpixel = 0;
  176.     SIOUXSettings.columns = 125;
  177.     SIOUXSettings.rows = 51;
  178.     SIOUXSetTitle ((unsigned char *) "\pM.A.C.N.A.C.E.");
  179. };
  180.